package my.edu.model.User.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import my.edu.common.domain.PageDTO;
import my.edu.common.domain.Response;
import my.edu.model.User.entity.UserRole;
import my.edu.model.User.entity.VO.LoginVO;
import my.edu.model.User.entity.dto.LoginDTO;
import my.edu.model.User.entity.dto.RegisterDTO;
import my.edu.model.User.service.IRoleService;
import my.edu.model.User.service.IUserRoleService;
import my.edu.model.User.service.IUserService;
import my.edu.security.shiroservice.ShiroServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.NoSuchAlgorithmException;


@RestController
@RequestMapping("/user")
@Tag(name = "登录接口")
public class LoginController {


    private IUserService userService;

    @Autowired
    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    @GetMapping("/publicKey")
    @Operation(summary = "获取公钥")
    public Response getPublicKey() throws NoSuchAlgorithmException {
        return Response.success(userService.getPublicKey());
    }


    @PostMapping("/login")
    @Operation(summary = "登录")
    public Response login(@RequestBody @Validated LoginDTO loginDTO) throws Exception {
        LoginVO vo = userService.login(loginDTO);
        return Response.success(vo);
    }

    @PutMapping("/register")
    @Operation(summary = "注册")
    public Response register(@RequestBody @Validated RegisterDTO registerDTO) {
        userService.register(registerDTO);
        return Response.success();
    }

    @GetMapping("/logout")
    @Operation(summary = "登出")
    public Response loginout() {
        return Response.success();
    }

}
